package com.glsc.ngateway.oaflow.service;


import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.oaflow.bean.xinyi.EUSP;
import com.glsc.ngateway.oaflow.bean.xinyi.EUSPOutput;
import com.glsc.ngateway.oaflow.bean.xinyi.EUSPParameter;
import com.glsc.ngateway.oaflow.bean.xinyi.EUSPServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.Map;

@Component
public class XyService {

    Logger logger = LoggerFactory.getLogger(XyService.class);

    @Value("${XY.SERVER_ADDR}")
    private String SERVER_ADDR ;

    @Value("${XY.USER_CODE}")
    private String USER_CODE ;

    @Value("${XY.PASS_WORD}")
    private String PASS_WORD ;


    public String callXy(String param) throws Exception {
        JSONObject paramObject = JSONObject.parseObject(param);
        String productId = paramObject.getString("productId");
        String serviceName = paramObject.getString("serviceName");
        String methodName = paramObject.getString("methodName");
        paramObject.remove("productId");
        paramObject.remove("serviceName");
        paramObject.remove("methodName");

        EUSP ei = new EUSPServiceLocator().getEUSPWs(new URL(SERVER_ADDR));

        EUSPOutput loginOutput = ei.login(USER_CODE, PASS_WORD);
        if(loginOutput.getRetCode() != 0) {
            throw new Exception(loginOutput.getRetMessage());
        }
        String token = loginOutput.getRetValue();


//        Map<String, String> paramMap = new HashMap<String, String>();
//        paramMap.put("OUT_INST_ID", "1");
//        paramMap.put("PALF_FLAG1", "12");
//        paramMap.put("SETTLE_UNIT_ID", "1");
//        paramMap.put("OCCUR_DATE", "20100803");
//        paramMap.put("SETTLE_DATE", "20100803");
//        paramMap.put("FT_DATE", "20100803");
//        paramMap.put("OCCUR_AMT", "100");
//        paramMap.put("INST_CLASS_ID", "1026");
//        paramMap.put("SETTLE_SELF_FUND_CODE", "9CCDCFT");
//        paramMap.put("BUSI_SCOPE", "02");
//        paramMap.put("FUND_PROP", "1");
//        paramMap.put("CUR_TYPE", "CNY");
//        paramMap.put("PAY_BANK_ACC_NAME", "建行自有资金账户");
//        paramMap.put("PAY_BANK_CODE", "0200");
//        paramMap.put("PAY_BANK_ACC", "0102001000000000904");
//        paramMap.put("PAY_OPEN_BANK_NAME", "1");
//        paramMap.put("REC_PROVIS_ACC_CODE", "CCDC001");
//        paramMap.put("REC_BANK_ACC_NAME", "CCDC子账户001");
//        paramMap.put("REC_BANK_CODE", "7904");
//        paramMap.put("REC_BANK_ACC", "CCDC001001");
//        paramMap.put("REC_OPEN_BANK_NAME", "中央登记");
//        paramMap.put("FT_DIRECT", "P");
//        paramMap.put("NOTE", "银行划中债100块");
//        paramMap.put("USER_CODE", "0007");

        Map<String, String> paramMap = JSONObject.parseObject(paramObject.toJSONString(),Map.class);
        EUSPParameter[] paramList = new EUSPParameter[paramMap.size()];
        int index = 0;
        for(Map.Entry<String, String> paramEntry : paramMap.entrySet()) {
            paramList[index++] = new EUSPParameter(paramEntry.getKey(), paramEntry.getValue());
        }
        try{
            logger.info("新意输入参数productId: " + JSONObject.toJSONString(productId));
            logger.info("新意输入参数serviceName: " + JSONObject.toJSONString(serviceName));
            logger.info("新意输入参数methodName: " + JSONObject.toJSONString(methodName));
            logger.info("新意输入参数paramList: " + JSONObject.toJSONString(paramList));


            EUSPOutput callOutput = ei.callService(token, productId, serviceName, methodName, paramList);
            //EUSPOutput{retCode=0, retMessage='操作成功[请求流水号：default task-71_1718594792542]', retValue='', rowsetCount=1, rowsets=[com.shine.test.eusp.EUSPRowSet@d5ce80bb], __equalsCalc=null, __hashCodeCalc=false}
            //EUSPOutput{retCode=-1, retMessage='外部指令编号[12345]的已经提交并生成指令，请勿重复提交![请求流水号：default task-63_1718594711098]', retValue='', rowsetCount=0, rowsets=null, __equalsCalc=null, __hashCodeCalc=false}
            logger.info("callOutput: " + JSONObject.toJSONString(callOutput));
            return JSONObject.toJSONString(callOutput);
        } catch (Exception e){
            //e.printStackTrace();
            logger.error("调用新意发生异常",e);
            throw e;
        } finally {
            ei.logout(token);
        }

//        try {
//            if(callOutput.getRetCode() == -1) {
//                throw new Exception(callOutput.getRetMessage());
//
//            } else {
//                System.out.println("RetCode: " + callOutput.getRetCode());
//                System.out.println("RetValue: " + callOutput.getRetValue());
//                System.out.println("RetMessage: " + callOutput.getRetMessage());
//
//                if(callOutput.getRowsetCount() > 0) {
//                    for(EUSPRowSet rowSet : callOutput.getRowsets()) {  //遍历复数的结果集
//                        int recordRow = rowSet.getRecordCount();
//
//                        if(recordRow > 0) { //遍历单个结果集
//                            EUSPRecord[] recordDataArr = rowSet.getRecords();
//                            String[] colNameArr = rowSet.getColumns();
//
//                            for(int recRow = 0; recRow < recordRow; recRow++) {
//                                System.out.println("返回结果集的第" + recRow + "行：");
//
//                                for(int colNum = 0, colCnt = colNameArr.length; colNum < colCnt; colNum++) {    //遍历结果集中的一行数据
//                                    String[] rowData = recordDataArr[recRow].getDatas();
//
//                                    System.out.println("列【" + colNameArr[colNum] + "】:=#" + rowData[colNum] + "$=");
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch(Exception e) {
//            e.printStackTrace();
//        }


    }



//    public static void main(String[] args) throws Exception {
//
//        EUSP ei = new EUSPServiceLocator().getEUSPWs(new URL(SERVER_ADDR));
//
//        EUSPOutput loginOutput = ei.login(USER_CODE, PASS_WORD);
//        if(loginOutput.getRetCode() != 0) {
//            throw new Exception(loginOutput.getRetMessage());
//        }
//        String token = loginOutput.getRetValue();
//
////        Map<String, String> paramMap = new HashMap<String, String>();
////        paramMap.put("OUT_INST_ID", "1");
////        paramMap.put("PALF_FLAG1", "12");
////        paramMap.put("SETTLE_UNIT_ID", "1");
////        paramMap.put("OCCUR_DATE", "20100803");
////        paramMap.put("SETTLE_DATE", "20100803");
////        paramMap.put("FT_DATE", "20100803");
////        paramMap.put("OCCUR_AMT", "100");
////        paramMap.put("INST_CLASS_ID", "1026");
////        paramMap.put("SETTLE_SELF_FUND_CODE", "9CCDCFT");
////        paramMap.put("BUSI_SCOPE", "02");
////        paramMap.put("FUND_PROP", "1");
////        paramMap.put("CUR_TYPE", "CNY");
////        paramMap.put("PAY_BANK_ACC_NAME", "建行自有资金账户");
////        paramMap.put("PAY_BANK_CODE", "0200");
////        paramMap.put("PAY_BANK_ACC", "0102001000000000904");
////        paramMap.put("PAY_OPEN_BANK_NAME", "1");
////        paramMap.put("REC_PROVIS_ACC_CODE", "CCDC001");
////        paramMap.put("REC_BANK_ACC_NAME", "CCDC子账户001");
////        paramMap.put("REC_BANK_CODE", "7904");
////        paramMap.put("REC_BANK_ACC", "CCDC001001");
////        paramMap.put("REC_OPEN_BANK_NAME", "中央登记");
////        paramMap.put("FT_DIRECT", "P");
////        paramMap.put("NOTE", "银行划中债100块");
////        paramMap.put("USER_CODE", "0007");
////
////        EUSPParameter[] paramList = new EUSPParameter[paramMap.size()];
////        int index = 0;
////        for(Entry<String, String> paramEntry : paramMap.entrySet()) {
////            paramList[index++] = new EUSPParameter(paramEntry.getKey(), paramEntry.getValue());
////        }
//        EUSPParameter[] param = new EUSPParameter[34];
//        param[0] = new EUSPParameter("SETTLE_UNIT_ID", "1");
//        param[1] = new EUSPParameter("FUND_PROP", "1");
//        param[2] = new EUSPParameter("SETTLE_SPEED", "T+0");
//        param[3] = new EUSPParameter("SEAT_CODE", "8888");
//        param[4] = new EUSPParameter("CUR_TYPE", "CNY");
//        param[5] = new EUSPParameter("MKT_CODE", "01");
//        param[6] = new EUSPParameter("SETTLE_TIME", "16:00");
//        param[7] = new EUSPParameter("SETTLE_AMT", "999");
//        param[8] = new EUSPParameter("PAY_BANK_CODE", "0200");
//        param[9] = new EUSPParameter("PAY_BANK_ACC", "32001618636050003568");
//        param[10] = new EUSPParameter("PAY_OPEN_BANK_NAME", "建行");
//        param[11] = new EUSPParameter("REC_BANK_ACC_NAME", "xxx");
//        param[12] = new EUSPParameter("REC_BANK_CODE", "0200");
//        param[13] = new EUSPParameter("REC_BANK_ACC", "1103020129200011120");
//        param[14] = new EUSPParameter("REC_OPEN_BANK_NAME", "工商");
//        param[15] = new EUSPParameter("FT_DATE", "20150116");
//        param[16] = new EUSPParameter("FT_NOTE", "划款");
//        param[17] = new EUSPParameter("LAST_FT_TIME", "16:00");
//        param[18] = new EUSPParameter("FT_DIRECT", "R");
//        param[19] = new EUSPParameter("INST_SRC", "3");
//        param[20] = new EUSPParameter("SETTLE_SELF_FUND_CODE", "300100001");
//        param[21] = new EUSPParameter("IS_SVAE_FT_INFO", "0");
//        param[22] = new EUSPParameter("SETTLE_SELF_FUND_TYPE", "1");
//        param[23] = new EUSPParameter("SETTLE_CHANNEL", "01");
//        param[24] = new EUSPParameter("FT_TYPE_CODE", "01");
//        param[25] = new EUSPParameter("OUT_INST_ID", "123456");
//        param[26] = new EUSPParameter("OCCUR_DATE", "20240614");
//        param[27] = new EUSPParameter("SETTLE_DATE", "20240614");
//        param[28] = new EUSPParameter("OCCUR_AMT", "12345.55");
//        param[29] = new EUSPParameter("NOTE", "摘要");
//        param[30] = new EUSPParameter("PAY_CUR_TYPE", "CNY");
//        param[31] = new EUSPParameter("INST_CLASS_ID", "1240");
//        param[32] = new EUSPParameter("PAY_BANK_ACC_NAME", "国联证券股份有限公司（客户）");
//        param[33] = new EUSPParameter("SETTLE_SELF_FUND_CODE", "9KX21360");
//
//
////        PAY_BANK_ACC_NAME	VARCHAR2(100)	付款银行账户名称	必填
////        PAY_BANK_CODE	VARCHAR2(30)	付款银行代码	必填
////        PAY_BANK_ACC	VARCHAR2(40)	付款银行账号	必填
////        PAY_OPEN_BANK_NAME	VARCHAR2(100)	付款开户行名称	必填
//
//
//
//
//
//        EUSPOutput callOutput = ei.callService(token, "13", "eafsif", "addStdFundInstReq", param);
//        System.out.println("callOutput: " + callOutput.toString());
//        //EUSPOutput{retCode=0, retMessage='操作成功[请求流水号：default task-71_1718594792542]', retValue='', rowsetCount=1, rowsets=[com.shine.test.eusp.EUSPRowSet@d5ce80bb], __equalsCalc=null, __hashCodeCalc=false}
//        //EUSPOutput{retCode=-1, retMessage='外部指令编号[12345]的已经提交并生成指令，请勿重复提交![请求流水号：default task-63_1718594711098]', retValue='', rowsetCount=0, rowsets=null, __equalsCalc=null, __hashCodeCalc=false}
//
//        try {
//            if(callOutput.getRetCode() == -1) {
//                throw new Exception(callOutput.getRetMessage());
//
//            } else {
//                System.out.println("RetCode: " + callOutput.getRetCode());
//                System.out.println("RetValue: " + callOutput.getRetValue());
//                System.out.println("RetMessage: " + callOutput.getRetMessage());
//
//                if(callOutput.getRowsetCount() > 0) {
//                    for(EUSPRowSet rowSet : callOutput.getRowsets()) {  //遍历复数的结果集
//                        int recordRow = rowSet.getRecordCount();
//
//                        if(recordRow > 0) { //遍历单个结果集
//                            EUSPRecord[] recordDataArr = rowSet.getRecords();
//                            String[] colNameArr = rowSet.getColumns();
//
//                            for(int recRow = 0; recRow < recordRow; recRow++) {
//                                System.out.println("返回结果集的第" + recRow + "行：");
//
//                                for(int colNum = 0, colCnt = colNameArr.length; colNum < colCnt; colNum++) {    //遍历结果集中的一行数据
//                                    String[] rowData = recordDataArr[recRow].getDatas();
//
//                                    System.out.println("列【" + colNameArr[colNum] + "】:=#" + rowData[colNum] + "$=");
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch(Exception e) {
//            e.printStackTrace();
//        }
//
//        ei.logout(token);
//    }


}